<template>
  <el-dialog
    title="修改积分"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item label="登录手机号:">
        <span>{{dataForm.loginAccount}}</span>
      </el-form-item>
      <el-form-item label="昵称:">
        <span>{{dataForm.nickName}}</span>
      </el-form-item>
      <el-form-item label="当前积分:">
        <span>{{dataForm.shopUserInfo.pointBalance}}</span>
        <span style="margin-left:5px">积分</span>
      </el-form-item>
      <el-form-item label="操作:" prop="oper">
        <el-radio-group v-model="dataForm.oper">
          <el-radio label="1" border>增加</el-radio>
          <el-radio label="2" border>减少</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="增加或减少的积分:" prop="point">
        <el-input v-model.number="dataForm.point" placeholder="请输入需要增加或减少的积分" @input="(v)=>(dataForm.point=v.replace(/[^\d]/g,''))"></el-input>
      </el-form-item>
      <el-form-item label=" ">
        <div style="color: #FF0000; font-size: 12px">
          <i class="el-icon-warning"></i>
          <span>注意：当前填写的积分是在原积分余额上增加或减少的积分</span>
        </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {platformUserPointChange} from '@/api/shop_user/user';
export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data(){
    return{
      canSubmit:false,
      dataForm:{
        oper:'',
        point:'',
        shopUserInfo:{}
      },
      dataRule: {
        oper: [
          { required: true, message: "操作不能为空", trigger: "blur" },
        ],
        point: [
          { required: true, message: "积分不能为空", trigger: "blur" },
          {required: false,validator: (rule, value, callback)=>{
            // 输入的场合，判断是否是数字
            if(this.dataForm.oper == '2' && parseInt(value) > parseInt(this.dataForm.pointBalance)){
              callback(new Error('减少的积分不能大于积分余额'));
            }
            callback();
          },  message: "", trigger: "change" }
        ],
      }
    }
  },
  methods:{

    /**
     * 初始化
     */
    init(row){
      this.canSubmit = true
      this.dataForm = JSON.parse(JSON.stringify(row))
    },

    /**
     * 修改积分
     */
    dataFormSubmit(){
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          let data = {
            shopUserId: this.dataForm.id,
            point: this.dataForm.point,
            oper: this.dataForm.oper
          }
          console.log(data)

          platformUserPointChange(data).then(res=>{
            this.$notify.success("修改成功");
            this.$emit("refreshDataList");
            this.closeDialog()
          })
        }
      })
    },

    /**
     * 关闭窗口
     */
    closeDialog() {
      this.$emit('update:visible', false)
    },
  }
}
</script>